package zhishuang.wang.sansi.tools.json.exception;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;

/**
 * @Author Zhishuang.Wang
 * @Date 2023/7/5 11:18
 * @Email 1406110602@qq.com
 */
public class FastjsonUtil {
    public FastjsonUtil() {
    }

    public static <V> V from(InputStream inputStream, Class<V> type) {
        try {
            return JSON.parseObject(inputStream, type, new Feature[0]);
        } catch (IOException var3) {
            throw new FastjsonException("fastjson from error, type: {}", new Object[]{type, var3});
        }
    }

    public static <V> V from(InputStream inputStream, TypeReference<V> typeReference) {
        try {
            return JSON.parseObject(inputStream, typeReference.getType(), new Feature[0]);
        } catch (IOException var3) {
            throw new FastjsonException("fastjson from error, type: {}", new Object[]{typeReference, var3});
        }
    }

    public static <V> V from(String json, Class<V> type) {
        return JSON.parseObject(json, type);
    }

    public static <V> V from(String json, TypeReference<V> typeReference) {
        return JSON.parseObject(json, typeReference.getType(), new Feature[0]);
    }

    public static <V> List<V> fromList(String json, Class<V> type) {
        return JSON.parseArray(json, type);
    }

    public static HashMap<String, Object> fromMap(String json) {
        return (HashMap)JSON.parseObject(json, new TypeReference<HashMap<String, Object>>() {
        }, new Feature[0]);
    }

    public static <V> String to(List<V> list) {
        return JSON.toJSONString(list);
    }

    public static <V> String to(V v) {
        return JSON.toJSONString(v);
    }

    public static String getAsString(String json, String key) {
        if (StringUtils.isEmpty(json)) {
            return null;
        } else {
            try {
                JSONObject jsonObject = JSON.parseObject(json);
                return jsonObject == null ? null : jsonObject.getString(key);
            } catch (Exception var3) {
                throw new FastjsonException("fastjson get string error, json: {}, key: {}", new Object[]{json, key, var3});
            }
        }
    }

    public static int getAsInt(String json, String key) {
        if (StringUtils.isEmpty(json)) {
            return 0;
        } else {
            try {
                JSONObject jsonObject = JSON.parseObject(json);
                return jsonObject == null ? 0 : jsonObject.getInteger(key);
            } catch (Exception var3) {
                throw new FastjsonException("fastjson get int error, json: {}, key: {}", new Object[]{json, key, var3});
            }
        }
    }

    public static long getAsLong(String json, String key) {
        if (StringUtils.isEmpty(json)) {
            return 0L;
        } else {
            try {
                JSONObject jsonObject = JSON.parseObject(json);
                return jsonObject == null ? 0L : jsonObject.getLong(key);
            } catch (Exception var3) {
                throw new FastjsonException("fastjson get long error, json: {}, key: {}", new Object[]{json, key, var3});
            }
        }
    }

    public static double getAsDouble(String json, String key) {
        if (StringUtils.isEmpty(json)) {
            return 0.0;
        } else {
            try {
                JSONObject jsonObject = JSON.parseObject(json);
                return jsonObject == null ? 0.0 : jsonObject.getDouble(key);
            } catch (Exception var3) {
                throw new FastjsonException("fastjson get double error, json: {}, key: {}", new Object[]{json, key, var3});
            }
        }
    }

    public static BigInteger getAsBigInteger(String json, String key) {
        if (StringUtils.isEmpty(json)) {
            return new BigInteger(String.valueOf(0.0));
        } else {
            try {
                JSONObject jsonObject = JSON.parseObject(json);
                return jsonObject == null ? new BigInteger(String.valueOf(0.0)) : jsonObject.getBigInteger(key);
            } catch (Exception var3) {
                throw new FastjsonException("fastjson get big integer error, json: {}, key: {}", new Object[]{json, key, var3});
            }
        }
    }

    public static BigDecimal getAsBigDecimal(String json, String key) {
        if (StringUtils.isEmpty(json)) {
            return new BigDecimal("0.0");
        } else {
            try {
                JSONObject jsonObject = JSON.parseObject(json);
                return jsonObject == null ? new BigDecimal("0.0") : jsonObject.getBigDecimal(key);
            } catch (Exception var3) {
                throw new FastjsonException("fastjson get big decimal error, json: {}, key: {}", new Object[]{json, key, var3});
            }
        }
    }

    public static boolean getAsBoolean(String json, String key) {
        if (StringUtils.isEmpty(json)) {
            return false;
        } else {
            try {
                JSONObject jsonObject = JSON.parseObject(json);
                return jsonObject == null ? false : jsonObject.getBooleanValue(key);
            } catch (Exception var3) {
                throw new FastjsonException("fastjson get boolean error, json: {}, key: {}", new Object[]{json, key, var3});
            }
        }
    }

    public static byte getAsByte(String json, String key) {
        if (StringUtils.isEmpty(json)) {
            return 0;
        } else {
            try {
                JSONObject jsonObject = JSON.parseObject(json);
                return jsonObject == null ? 0 : jsonObject.getByteValue(key);
            } catch (Exception var3) {
                throw new FastjsonException("fastjson get bytes error, json: {}, key: {}", new Object[]{json, key, var3});
            }
        }
    }

    public static <V> V getAsObject(String json, String key, Class<V> type) {
        if (StringUtils.isEmpty(json)) {
            return null;
        } else {
            try {
                JSONObject jsonObject = JSON.parseObject(json);
                return jsonObject == null ? null : JSON.parseObject(jsonObject.getString(key), type);
            } catch (Exception var4) {
                throw new FastjsonException("fastjson get list error, json: {}, key: {}, type: {}", new Object[]{json, key, type, var4});
            }
        }
    }

    public static <V> List<V> getAsList(String json, String key, Class<V> type) {
        if (StringUtils.isEmpty(json)) {
            return null;
        } else {
            try {
                JSONObject jsonObject = JSON.parseObject(json);
                if (jsonObject == null) {
                    return null;
                } else {
                    JSONArray jsonArray = jsonObject.getJSONArray(key);
                    return jsonArray.toJavaList(type);
                }
            } catch (Exception var5) {
                throw new FastjsonException("fastjson get list error, json: {}, key: {}, type: {}", new Object[]{json, key, type, var5});
            }
        }
    }

    public static JSONObject getAsJsonObject(String json, String key) {
        if (StringUtils.isEmpty(json)) {
            return null;
        } else {
            try {
                JSONObject jsonObject = JSON.parseObject(json);
                return jsonObject == null ? null : jsonObject.getJSONObject(key);
            } catch (Exception var3) {
                throw new FastjsonException("fastjson get list error, json: {}, key: {}", new Object[]{json, key, var3});
            }
        }
    }

    public static <V> String add(String json, String key, V value) {
        JSONObject jsonObject = JSON.parseObject(json);
        add(jsonObject, key, value);
        return jsonObject.toString();
    }

    private static <V> void add(JSONObject jsonObject, String key, V value) {
        if (!(value instanceof String) && !(value instanceof Number) && !(value instanceof Boolean) && !(value instanceof Byte[])) {
            jsonObject.put(key, to(value));
        } else {
            jsonObject.put(key, value);
        }

    }

    public static String remove(String json, String key) {
        JSONObject jsonObject = JSON.parseObject(json);
        jsonObject.remove(key);
        return jsonObject.toString();
    }

    public static <V> String update(String json, String key, V value) {
        JSONObject jsonObject = JSON.parseObject(json);
        add(jsonObject, key, value);
        return jsonObject.toString();
    }

    public static String format(String json) {
        JSONObject jsonObject = JSON.parseObject(json);
        return JSON.toJSONString(jsonObject, new SerializerFeature[]{SerializerFeature.PrettyFormat});
    }

    public static boolean isJson(String json) {
        try {
            JSON.parse(json);
            return true;
        } catch (Exception var2) {
            return false;
        }
    }
}
